<?php
/**
 * @author shenjun
 * @todo session部份的处理信息
 * @createdate 2009-11-27
 * @copyright 0.9 
 *
 */
class session{
	
	private function __construct($type){
		$func_name='_'.$type;
		if (method_exists($this,$func_name)) {
			$this->$func_name();
		}
	}
	
	protected function _start(){
		#设置session_name
		$this->set_session_name();
		#设置session存储路径
		$this->set_session_path();
		#设置session是否支持二级域名
		$this->set_session_com();
		session_start();
		#设置session生成的一些信息
		$this->set_session_data();
	}
	
	protected function _destory(){
		session_destroy();
	}
	
	protected function set_session_name(){
		$config=&get_config();
		if($config->session_name===true){
			$agent=$_SERVER['HTTP_USER_AGENT'];
			$ip=getIP();
			$name=hash('crc32',$agent.$ip);
		}else{
			$name='PHPSESSID';
		}
		session_name($name);
	}
	
	protected function set_session_path(){
		if(get_config('session_save')){
			session_save_path(DOCUMENT_ROOT.'/'.get_config('session_path'));
		}
	}
	
	protected function set_session_data(){
		$_SESSION['imvc']['times']=intval($_SESSION['imvc']['times'])==0 ? 1 : $_SESSION['imvc']['times']+1;
		$_SESSION['imvc']['ip']=getIP();
		$_SESSION['imvc']['agent']=$_SERVER['HTTP_USER_AGENT'];
		$_SESSION['imvc']['create']=empty($_SESSION['imvc']['create']) ? strtotime('now') : $_SESSION['imvc']['create'];
		$_SESSION['imvc']['lasttime']=strtotime('now');
	}
	
	protected function set_session_com(){
		if (get_config('session_com')) {
			session_set_cookie_params(0,'/',substr($_SERVER['HTTP_HOST'],strpos($_SERVER['HTTP_HOST'],'.')));
		}/*else{
			session_set_cookie_params(0,'/',$_SERVER['HTTP_HOST']);
		}*/
	}
	
	static function start(){
		new session('start');
	}
	
	static function destory(){
		new session('destory');
	}
}